<?php
use \waf\log\Log;
use \waf\server\UdpServer;

class UdpAgentServer extends UdpServer
{
    public $objClient;

    public function onWorkerStart($objSServer, $workerId)
    {
        Log::logSystem("onWorkerStart", __FILE__, __LINE__, __FUNCTION__);

        parent::onWorkerStart($objSServer, $workerId);
        $this->initClient();
        swoole_timer_tick(5000, [$this, 'tickCheckAgentClient']);
    }

    public function tickCheckAgentClient($time_id, $params = null)
    {
        Log::logSystem("time_id=".$time_id, __FILE__, __LINE__, __FUNCTION__);

        if(!$this->objClient || !$this->objClient->isConnected())
        {
            $this->initClient();
        }
    }

    public function initClient()
    {
        Log::logSystem("initClient", __FILE__, __LINE__, __FUNCTION__);
        $this->objClient = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);
        $this->objClient->on("connect", function($cli) {
            Log::logSystem("connect", __FILE__, __LINE__, __FUNCTION__);
        });

        $this->objClient->on("receive", function($cli, $data) {
            Log::logSystem("receive", __FILE__, __LINE__, __FUNCTION__);
        });

        $this->objClient->on("error", function($cli){
            Log::logSystem("error", __FILE__, __LINE__, __FUNCTION__);
        });

        $this->objClient->on("close", function($cli){
            Log::logSystem("close", __FILE__, __LINE__, __FUNCTION__);
        });

        $this->objClient->connect('127.0.0.1', 9501, 0.5);
    }
}
?>